package com.guoxue.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.guoxue.dao.ITapesRecordsDao;
import com.guoxue.domain.TapesRecordsVo;
import com.guoxue.model.RankModel;
@Repository
public class TapesRecordsDaoImpl extends BaseDaoImpl<TapesRecordsVo> implements ITapesRecordsDao {

	@Override
	public void saveTapesRecords(TapesRecordsVo tr) {
		saveOrUpdate(tr);
	}
	
	public void deleteByTimeAndStatus(String date) {
		executeHql("DELETE FROM TapesRecordsVo t WHERE t.sysflag=1 AND t.deleteTime<?", new Object[] {date});
	}
	
	public List<TapesRecordsVo> findByTimeAndStatus(String date) {
		return find("FROM TapesRecordsVo t WHERE t.sysflag=1 AND t.deleteTime<?",new Object[] {date});
	}

	@Override
	public List<TapesRecordsVo> findByUidAndStatus(int uid, int sysflag) {
		return find("FROM TapesRecordsVo t WHERE t.uid=? AND t.sysflag=? ORDER BY t.commitTime DESC",new Object[] {uid,sysflag});
	}
	
	@Override
	public Long getTapesRecordsTotal(int uid,String flag) {
		return count("SELECT count(*) FROM TapesRecordsVo t WHERE t.uid = ? AND t.tapes = ? AND t.sysflag=0",new Object[] {uid,flag});
	}

	@Override
	public TapesRecordsVo getTapesById(int tapesId) {
		return get("FROM TapesRecordsVo t WHERE t.id=? AND t.sysflag=0", new Object[] {tapesId});
	}

	@Override
	public TapesRecordsVo getTapesByChapterAndUid(String chapter, Integer uid) {
		return get("FROM TapesRecordsVo t WHERE t.chapter=? AND t.uid=? AND t.sysflag=0", new Object[] {chapter,uid});
	}

	@Override
	public int getTapesCounts(Integer uid) {
		int num = count("SELECT count(*) FROM TapesRecordsVo t WHERE t.uid = ? AND t.sysflag=0",new Object[] {uid}).intValue();
		return num;
	}

	@Override
	public List<RankModel> getLocalGift(int did, int type) {
		String sql = "";
		if (type==1){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM tapesrecordsvo ta, uservo u, tapessharevo ts WHERE ta.id=ts.tapesId AND ta.uid=u.id AND ta.sysflag=0 "
					+ "AND u.district=? ORDER BY ts.weeklyGifts DESC";
		}
		if (type==2){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM tapesrecordsvo ta, uservo u, tapessharevo ts WHERE ta.id=ts.tapesId AND ta.uid=u.id AND ta.sysflag=0 "
					+ "AND u.district=? ORDER BY ts.weeklyListend DESC";
		}                                           
		return super.getLocalRank(sql, new Object[] {did});
	}

	@Override
	public List<RankModel> getTotalGift(int type) {
		String sql = "";
		if (type==1){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM tapesrecordsvo ta, uservo u, tapessharevo ts WHERE ta.id=ts.tapesId AND ta.uid=u.id AND ta.sysflag=0 "
					+ "ORDER BY ts.totalGifts DESC";
		}
		if (type==2){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM tapesrecordsvo ta, uservo u, tapessharevo ts WHERE ta.id=ts.tapesId AND ta.uid=u.id AND ta.sysflag=0 "
					+ "ORDER BY ts.totalListend DESC";
		}
		return super.getTotalRank(sql);
	}

	@Override
	public List<RankModel> getTotalWeeklyGift(int type) {
		String sql = "";
		if (type==3){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM tapesrecordsvo ta, uservo u, tapessharevo ts WHERE ta.id=ts.tapesId AND ta.uid=u.id AND ta.sysflag=0 "
					+ "ORDER BY ts.weeklyTotalGifts DESC ";
		}
		if (type==4){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM tapesrecordsvo ta, uservo u, tapessharevo ts WHERE ta.id=ts.tapesId AND ta.uid=u.id AND ta.sysflag=0 "
					+ "ORDER BY ts.weeklyTotalListend DESC ";
		}
		return super.getTotalRank(sql);
	}

	@Override
	public List<RankModel> getLocalWeekly(int district, int type) {
		String sql = "";
		if (type==3){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM TapesRecordsVo ta, UserVo u, TapesShareVo ts WHERE u.id=ta.uid AND ta.id=ts.tapesId AND ta.sysflag=0 "
					+ "AND u.district=? ORDER BY ts.weeklyTotalGifts DESC";
		}
		if (type==4){
			sql = "SELECT ta.id,ta.uid,ta.chapter,u.nickName, u.imgUrl, ts.weeklyGifts, ts.weeklyListend"
					+ ", ts.weeklyTotalGifts, ts.weeklyTotalListend, ts.totalGifts, ts.totalListend FROM TapesRecordsVo ta, UserVo u, TapesShareVo ts WHERE u.id=ta.uid AND ta.id=ts.tapesId AND ta.sysflag=0 "
					+ "AND u.district=? ORDER BY ts.weeklyTotalListend DESC";
		}
		return super.getLocalRank(sql, new Object[] {district});
	}

	@Override
	public List<TapesRecordsVo> getTapesForCount(int start, int total, int uid) {
		String sql = "SELECT t.id, t.chapter,t.commitTime, t.flag, t.size, t.tapes,t.top,t.uid"
				+ ",t.userComment FROM TapesRecordsVo t where t.uid=? AND t.sysflag=0 ORDER BY t.commitTime DESC";
		return super.getTapesForCount(sql,start,total,uid);
	}

	@Override
	public List<TapesRecordsVo> getTapesForCounBytime(int start, int total, int userid, String date) {
		String sql = "SELECT t.id, t.chapter,t.commitTime, t.flag, t.size, t.tapes,t.top,t.uid"
				+ ",t.userComment FROM TapesRecordsVo t where t.uid=? AND t.commitTime<=? AND t.sysflag=0 ORDER BY t.commitTime DESC";
		
		return super.getTapesForCount(sql,start,total,userid,date);
	}

	
}
